package com.vmix.vmixmobiledemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.app.Activity;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import android.view.View;
import android.view.ViewGroup;

import com.vmix.android.vmixsdk.valueobject.VmixVideos;
import com.vmix.vmixmobiledemo.ImageLoader;

public class VmixVideoListAdapter extends BaseAdapter {
	
	private Activity activity;
	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader;
	
	// response from GetCollectionMediasService
	private VmixVideos videos;

	/**
	 * Constructor for the VideoListAdapter
	 * 
	 * @param a
	 * @param videos
	 */
	public VmixVideoListAdapter( Activity a, VmixVideos videos ) {
		this.activity = a;
		this.videos = videos;
		
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(activity.getApplicationContext());
	}
	
	// implement getCount
	public int getCount() {
        return videos.size();
    }

	// implement getItem
    public Object getItem(int position) {
        return this.videos.get( position );
    }

    // implement getItemId
    public long getItemId(int position) {
        return position;
    }
	
    /**
     * Custom view holder
     * 
     * @author lei
     *
     */
    public static class ViewHolder{
    	public TextView text;
    	public TextView description;
    	public ImageView image;
    }

    /**
     * Implement getVIew
     */
    public View getView(int position, View convertView, ViewGroup parent) {
    	View vi=convertView;
    	ViewHolder holder;
		
    	if(convertView==null){
			vi = inflater.inflate(R.layout.video_cell, null);
			holder=new ViewHolder();
			holder.text=(TextView)vi.findViewById(R.id.text);
			holder.description = (TextView)vi.findViewById(R.id.description);
			holder.image=(ImageView)vi.findViewById(R.id.image);
			vi.setTag(holder);
		}
		else {
			holder = (ViewHolder)vi.getTag();
		}
		
		//holder.text.setText("item "+position);
		String title = this.videos.get(position).getTitle();
		String description = this.videos.get(position).getDescription();
		String thumbnail = this.videos.get(position).getThumbnail();
		
		// set title, description and thumbnail for the video
		holder.text.setText( title );
		holder.description.setText( description );
		
		holder.image.setTag(thumbnail);
		imageLoader.DisplayImage(thumbnail, activity, holder.image);
        
		return vi;
    }

}
